<template>
  <div class="sub-box-border body" >
    <div class="left-align sub-title">作业集</div>
    <el-divider></el-divider>
    <div class="margin flex-space-around">
      <div>
        <el-button type="success" icon="el-icon-edit" @click="dialogFormVisible = true">创建新的作业集</el-button>
      </div>
      <div style="width: 400px;">
        <el-row :gutter="10">
          <el-col :span="9">
            <el-input size="mini" v-model="table.queryText" placeholder="名称/备注"></el-input>
          </el-col>
          <el-col :span="3">
            <el-button size="mini" type="warning" @click="handleQuery">查询</el-button>
          </el-col>
        </el-row>
      </div>
    </div>
    <div>
      <el-table
        v-loading="loading"
        :data="homeworkCollection"
        style="width: 100%">
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column
          label="名称"
          width="180">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>介绍: {{ scope.row.collectionIntroduction }}</p>
              <p>要求: {{ scope.row.collectionRequest }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.collectionName }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          label="开始日期"
          width="180">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.collectionStartTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="结束日期"
          width="180">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.collectionEndTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="submitCount"
          label="提交人数"
          sortable
          width="180">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="checkCollection(scope.$index, scope.row)">查看</el-button>
            <el-popconfirm
              title="这是一段内容确定删除吗？"
              @onConfirm="handleDelete(scope.$index, scope.row)"
            >
              <el-button
                size="mini"
                type="danger"
                slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="table.currentPage"
        :page-sizes="[5, 10]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="table.total">
      </el-pagination>
    </div>
    <div>
      <el-dialog title="创建新的作业集" :visible.sync="dialogFormVisible" width="500px">
        <el-form>
          <el-form-item label="名称" label-width="200">
            <el-input v-model="newCollection.collectionName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="介绍" label-width="200">
            <el-input v-model="newCollection.collectionIntroduction" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="要求" label-width="200" label-height="200">
            <el-input type="textarea" rows="5" v-model="newCollection.collectionRequest" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="日期" label-width="200">
            <div class="block">
              <span class="demonstration">带快捷选项</span>
              <el-date-picker
                v-model="newCollection.tempTime"
                type="datetimerange"
                :picker-options="pickerOptions"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="new Date()"
                value-format="yyyy-MM-dd HH:mm:ss"
                align="right">
              </el-date-picker>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCollection()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
    export default {
        name: "HomeworkCollection",
        data() {
          return {
            loading: false,
            userId: '',
            homeworkCollection:[],
            dialogFormVisible: false,
            newCollection: {
              tempTime: '',  //这个时间属性作为数组绑定新作业集的时间,在发送时需要转交给collectionStartTime,collectionEndTime
              collectionName: '',
              collectionIntroduction:'按时提交',
              collectionRequest: '按时提交',
              collectionStartTime:'',
              collectionEndTime:''
            },
            table:{
              queryText: '',
              pageSize: 5,
              currentPage: 1,
              total: 0
            },
            pickerOptions: {
              shortcuts: [{
                text: '最近一周',
                onClick(picker) {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                  picker.$emit('pick', [start, end]);
                }
              }, {
                text: '最近一个月',
                onClick(picker) {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                  picker.$emit('pick', [start, end]);
                }
              }, {
                text: '最近三个月',
                onClick(picker) {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                  picker.$emit('pick', [start, end]);
                }
              }]
            },
          }
        },
        methods:{
          //分页
          handleQuery: function () {
            this.getTable();
          },
          handleSizeChange: function (size) {
            let data=this.$data;
            data.table.pageSize = size;
            this.getTable();
          },
          handleCurrentChange: function (currentPage) {
            let data=this.$data;
            data.table.currentPage = currentPage;
            this.getTable();
          },
          getTable(){
            //获得所有的列表信息
            let that = this;
            let data=this.$data;
            let qs = require("qs");
            data.loading = true;
            this.axios.post('/collection/showtable',qs.stringify({
              userId: data.userId,
              queryText: data.table.queryText,
              pageSize: data.table.pageSize,
              currentPage: data.table.currentPage
            }))
            .then(response => {
              let resData = response.data;
              if(resData.errCode==200){
                data.homeworkCollection = resData.data.listTable;
                data.table.total = resData.data.total;
              }else{
                that.$message({
                  type: "error",
                  message: resData.errMsg
                })
              }
              data.loading = false;
            });
          },
          handleDelete(index, row){
            let data = this.$data;
            let qs = require("qs");
            let that = this;
            this.axios.post("/collection/delete",qs.stringify({
              userId: row.userId,
              collectionId: row.collectionId,
              collectionIsDelete: 1
            }))
            .then(response => {
              let resData = response.data;
              if(resData.errCode == 200){
                this.$message({
                  type: "success",
                  message: resData.data
                });
                that.getTable();
              }else{
                this.$message({
                  type: "error",
                  message: resData.errMsg
                })
              }
            })
          },
          addCollection(){
            let data = this.$data;
            let that = this;
            let qs = require("qs");
            data.loading = true;
            data.newCollection.userId = that.$store.state.user.userId;
            data.newCollection.collectionStartTime = data.newCollection.tempTime[0];
            data.newCollection.collectionEndTime = data.newCollection.tempTime[1];
            this.axios.post("/collection/add",qs.stringify(data.newCollection))
            .then(response => {
              let resData=response.data;
              if(resData.errCode==200){
                this.$message({
                  type: "success",
                  message: resData.data
                });
                this.getTable();
                data.dialogFormVisible = false;
              }else{
                this.$message({
                  type: "error",
                  message: resData.errMsg
                })
              }
              data.loading = false;
            })
          },
          checkCollection(index,row){
            this.$router.push({
              name: "collectionInformation",
              params: {
                collectionId: row.collectionId,
              }
            })
          }
        },
        created:function(){
            let data = this.$data;
            data.userId = this.$store.state.user.userId;
            this.getTable();
        }
    }
</script>

<style scoped>
  .body{
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
  }

  .flex-space-around{
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 600px;
  }
</style>
